package com.cbh.domain.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbh.common.id.IdWorker;
import com.cbh.entity.InsuranceCompany;
import com.cbh.mapper.InsuranceCompanMapper;

@Service
public class InsuranceCompanyService {
	private static final Log logger = LogFactory.getLog(InsuranceCompanyService.class);
	@Autowired
	InsuranceCompanMapper icMapper;

	/**
	 * 同时处理新增和更新 新增时需要判断code是否唯一 更新时判断code是否能够使用
	 * @param ic
	 * @return
	 * @throws Exception
	 */
	public String save(InsuranceCompany ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}

		// 根据code获取保险公司信息
		InsuranceCompany company = getIcByCode(ob.getIcCode());
		// 新增
		if (StringUtils.isEmpty(ob.getIcId()) && !StringUtils.isEmpty(ob.getIcCode())) {
			if (null == company) {
				return insert(ob);
			} else {
				throw new Exception("icCode已存在");
			}
		}
		// 更新
		if (!StringUtils.isEmpty(ob.getIcId())) {
			if (null != company && !company.getIcId().equals(ob.getIcId())) {
				throw new Exception("icCode已存在");
			}
			return update(ob);
		}
		throw new Exception("异常操作");
	}

	/**
	 * 新增保险公司
	 * 
	 * @param ic
	 * @return
	 * @throws Exception
	 */
	public String insert(InsuranceCompany ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setIcId(IdWorker.getId().toString());
		try {
			Integer re = icMapper.insert(ob);
			if (re > 0) {
				return ob.getIcId();
			} else {
				throw new Exception("保存数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("保存数据失败");
		}
	}

	/**
	 * 更新保险公司
	 * 
	 * @param ic
	 * @return
	 * @throws Exception
	 */
	public String update(InsuranceCompany ob) throws Exception {
		try {
			Integer re = icMapper.update(ob);
			if (re > 0) {
				return ob.getIcId();
			} else {
				throw new Exception("更新数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("更新数据失败");
		}
	}

	/**
	 * 根据icCode获取保险公司信息
	 * 
	 * @param IcCode
	 * @return
	 * @throws Exception
	 */
	public InsuranceCompany getIcByCode(String code) throws Exception {
		if (StringUtils.isEmpty(code)) {
			return null;
		}
		InsuranceCompany ob = null;
		try {
			List<InsuranceCompany> obs = icMapper.queryAll(new InsuranceCompany().code(code));
			if (null != obs && !obs.isEmpty())
				ob = obs.get(0);
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ob;
	}

	/**
	 * 根据icId获取保险公司信息
	 * 
	 * @param icId
	 * @return
	 * @throws Exception
	 */
	public InsuranceCompany getOne(String id) throws Exception {
		if (null == id) {
			throw new Exception("主键为空");
		}

		InsuranceCompany one = null;
		try {
			one = icMapper.queryOne(new InsuranceCompany().id(id));
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return one;
	}

	/**
	 * 获取保险公司列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<InsuranceCompany> companies() throws Exception {
		List<InsuranceCompany> ids = null;
		try {
			ids = icMapper.queryAll(new InsuranceCompany());
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ids;
	}

	/**
	 * 删除保险公司
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean delete(String id) throws Exception {
		try {
			Integer re = icMapper.delect(new InsuranceCompany().id(id));
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}
}
